

package com.hazelcast.partition.strategy;

import com.hazelcast.internal.serialization.SerializableByConvention;
import com.hazelcast.partition.PartitionAware;
import com.hazelcast.partition.PartitioningStrategy;

@SerializableByConvention
@SuppressWarnings("JavadocVariable")
public final class StringAndPartitionAwarePartitioningStrategy implements PartitioningStrategy {

    //since the StringAndPartitionAwarePartitioningStrategy is stateless, we can just create an instance up front
    //and keep reusing that. No need to create an instance of this class if it isn't needed.
    public static final StringAndPartitionAwarePartitioningStrategy INSTANCE = new StringAndPartitionAwarePartitioningStrategy();

    //we don't want instance.
    private StringAndPartitionAwarePartitioningStrategy() {
    }

    @Override
    public Object getPartitionKey(Object key) {
        if (key instanceof String string) {
            return StringPartitioningStrategy.getPartitionKey(string);
        } else
            if (key instanceof PartitionAware aware) {
                return aware.getPartitionKey();
            }
        return null;
    }
}
